#include "screenmanager.h"
#include "src/subwindow.h"
#include "src/mainwindow.h"
#include <QWindow>
#include <QDebug>

ScreenManager::ScreenManager(QObject *parent) : QObject(parent)
{
    mPrimaryWidget = nullptr;
    connect(qApp, &QGuiApplication::primaryScreenChanged, this, &ScreenManager::onPrimaryScreenChanged);
    connect(qApp, &QGuiApplication::screenAdded, this, &ScreenManager::onScreenAdded);
    connect(qApp, &QGuiApplication::screenRemoved, this, &ScreenManager::onScreenRemoved);
    cur_w = -1;
}


void ScreenManager::setPrimaryScreenWidget(QMainWindow* widget)
{
    if (widget == nullptr)
        return;
    mPrimaryWidget = widget;
}

void ScreenManager::setPrimaryScreenWidget(QMainWindow* widget, QList<QString> styles)
{
    if (widget == nullptr)
        return;
    mPrimaryWidget = widget;
    mPrimaryWindowStyles = styles;
}
void ScreenManager::setSubScreenWidget(QMainWindow* widget)
{
    if (widget == nullptr)
        return;
    mListSubwindow.append(widget);
}

void ScreenManager::showFullScreenAll()
{
    QScreen* primaryScreeen = QApplication::primaryScreen();
    QRect primaryRect = primaryScreeen->geometry();
    qDebug() << "primaryScreeen" <<primaryScreeen->name() << primaryRect;

//    MainWindow* mainwindow = qobject_cast<MainWindow*>(mPrimaryWidget);
//    if (primaryRect.width() >= 1920)
//    {   mainwindow->initResolution(Resolution_1920_1080);    cur_w = 0; }
//    else if (primaryRect.width() <= 1366)
//    {   mainwindow->initResolution(Resolution_1366_768);    cur_w = 2; }
//    else
//    {   mainwindow->initResolution(Resolution_1600_900);    cur_w = 1; }


//    QString style = mPrimaryWindowStyles.at(cur_w);
//    qApp->setStyleSheet(style);

    mPrimaryWidget->windowHandle()->setScreen(primaryScreeen);
//    mPrimaryWidget->hide();
//    mPrimaryWidget->setGeometry(primaryRect);
//    mPrimaryWidget->setWindowState(Qt::WindowNoState);
//    mPrimaryWidget->setFixedSize(primaryRect.size());
//    mPrimaryWidget->move(primaryRect.topLeft());

//    mPrimaryWidget->setWindowState(Qt::WindowFullScreen);
//    mPrimaryWidget->setVisible(true);
//    mPrimaryWidget->move(primaryRect.topLeft());

    mPrimaryWidget->resize(primaryScreeen->size());
    mPrimaryWidget->move(primaryRect.topLeft());
    mPrimaryWidget->show();

//    qDebug() << "mainwin show pos: "<< mPrimaryWidget->geometry() << " primaryScreeen geo: " << primaryScreeen->geometry();

    QList<QScreen*> listScreen = QApplication::screens();
    if (listScreen.size() > mListSubwindow.size()+1)
        return;

    int indexSubwin = 0;
    for (QScreen* screen :listScreen)
    {
        if (screen == primaryScreeen)
            continue;
        QRect subRect = screen->geometry();
        if (primaryRect.topLeft() == subRect.topLeft() ) // || primaryRect.intersects(subRect) || primaryRect.contains(subRect))
            continue;
        QMainWindow* subWin = mListSubwindow.at(indexSubwin++);
        subWin->windowHandle()->setScreen(screen);
//        subWin->hide();
//        subWin->setGeometry(subRect);
//        subWin->setWindowState(Qt::WindowNoState);
//        subWin->setFixedSize(subRect.size());
//        subWin->move(subRect.topLeft());

        subWin->setWindowState(Qt::WindowFullScreen);
        subWin->setVisible(true);
        subWin->move(subRect.topLeft() - primaryRect.topLeft());

//        subWin->resize(screen->size());
//        subWin->move(screen->geometry().topLeft());
//        subWin->show();

        qDebug() << "subwin show pos: "<< subWin->geometry() << " screen geo: " << screen->geometry();
    }

    for ( ; indexSubwin < mListSubwindow.size(); indexSubwin++)
    {
        QMainWindow* subWin = mListSubwindow.at(indexSubwin);
        subWin->setWindowState(Qt::WindowMinimized);
        subWin->setVisible(false);
    }

}

void ScreenManager::onPrimaryScreenChanged(QScreen* screen)
{
    qDebug() << "PrimaryScreenChanged: "<< screen->name() << screen->geometry() << qApp->primaryScreen()->geometry();
    showFullScreenAll();
}
void ScreenManager::onScreenAdded(QScreen* screen)
{
    qDebug() << "ScreenAdded: "<< screen->name() << screen->geometry();
//    @todo
//    QWidget* newWidget = nullptr;
//    setSubScreenWidget(screen, newWidget);
}
void ScreenManager::onScreenRemoved(QScreen* screen)
{
    qDebug() << "ScreenRemoved: "<< screen->name() << screen->geometry();

    for (QMainWindow* subwin : mListSubwindow)
    {
        if (subwin->windowHandle()->screen() == screen)
        {
            mListSubwindow.removeOne(subwin);
            subwin->setVisible(false);
            break;
        }
    }
    // keep at least 2 subw
//    if (mListSubwindow.size() > 2)
//    {
//        QMainWindow* subLast = mListSubwindow.takeLast();
//        // delete subLast;
//    }
    showFullScreenAll();
}

int ScreenManager::curMainWindow() {
    return cur_w;
}

